/**

 * @author admin

 * 2014年7月16日

 */

package com.tmall.testgen.plugin.wizards.runinspring.listener;

import java.util.List;

import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.widgets.Shell;

import com.tmall.testgen.client.ClientDataCenter;
import com.tmall.testgen.common.bo.interact.back.InterceptInfo;
import com.tmall.testgen.plugin.views.vo.SpringTree;
import com.tmall.testgen.plugin.wizards.dialog.TestDescriptionDialog;

public class TestDescriptionInputListener implements  SelectionListener{
	TreeViewer parasTreeViewer;
	Shell shell;
	
	public TestDescriptionInputListener(TreeViewer parasTreeViewer,Shell shell){
		this.parasTreeViewer=parasTreeViewer;
		this.shell=shell;
	}
	@Override
	public void widgetSelected(SelectionEvent e) {
		IStructuredSelection selection = (IStructuredSelection) parasTreeViewer
				.getSelection();
		SpringTree currentTree = (SpringTree) selection .getFirstElement();
		TestDescriptionDialog dialog = new TestDescriptionDialog(shell, "单元测试描述输入框", "请输入单元测试描述：", getTestDesFromTree(currentTree),currentTree);
		int returnInt = dialog.open();
		if(returnInt==Window.OK){
			//点击ok处理
			dealOk( currentTree, dialog);
		}
	}
	
	private String getTestDesFromTree(SpringTree currentTree){
	    if (currentTree == null) {
            return "";
        }
        if(currentTree.getValue()==null||!(currentTree.getValue() instanceof InterceptInfo)){
            return "";
        }
        
        InterceptInfo tem= (InterceptInfo) currentTree.getValue();
        
        List<InterceptInfo> infoes=ClientDataCenter.getInstance().getInfoes();
        for(InterceptInfo info:infoes){
            if(tem.equals(info)){
                if(tem.getTestDes()==null)
                    return "";
                else
                    return tem.getTestDes();
            }
        }
        
        return "";
	}
	private void dealOk(SpringTree currentTree,TestDescriptionDialog dialog){
		Object result =dialog.getResult();
		
        if (currentTree == null) {
            return;
        }
        if(currentTree.getValue()==null||!(currentTree.getValue() instanceof InterceptInfo)){
            return;
        }
        
        InterceptInfo tem= (InterceptInfo) currentTree.getValue();
        
        List<InterceptInfo> infoes=ClientDataCenter.getInstance().getInfoes();
        for(InterceptInfo info:infoes){
            if(tem.equals(info)){
                info.setTestDes(result+"");
                break;
            }
        }
	}

	@Override
	public void widgetDefaultSelected(SelectionEvent e) {
	}

}
